package com.jiarong.mapper;

import com.jiarong.domain.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author 加绒
 * @date 2021/10/27 11:05
 */
public interface UserMapper {
    /**
     * 新增用户
     *
     * @param user 用户信息
     */
    @Insert("insert into user(username, password, birthday) values (#{username}, #{password}, #{birthday})")
    void insertUser(User user);

    /**
     * 修改用户
     *
     * @param user 用户信息
     */
    @Update("update user set username = #{username}, password = #{password} where user_id = #{userId}")
    @Result(id = true, column = "user_id", property = "userId")
    void updateUser(User user);

    /**
     * 根据id删除用户
     *
     * @param userId 用户id
     */
    @Delete("delete from user where user_id = #{userId}")
    @Result(id = true, column = "user_id", property = "userId")
    void deleteUserById(Integer userId);

    /**
     * 查询所有
     *
     * @return 用户信息集合
     */
    @Select("select * from user")
    @Result(id = true, column = "user_id", property = "userId")
    List<User> selectAll();

    /**
     * 根据id查询
     *
     * @param userId 用户id
     * @return 用户信息信息
     */
    @Select("select * from user where user_id = #{userId}")
    @Result(id = true, column = "user_id", property = "userId")
    User selectById(Integer userId);

    /**
     * 根据id批量查询
     *
     * @param userId 用户id数组
     * @return 用户信息集合
     */
    @Select({
            "<script>",
            "select * from user where ",
            "<foreach collection='array' open='user_id in (' close=')' item='userId' separator=','>",
            "#{userId}",
            "</foreach>",
            "</script>"
    })
    @Result(id = true, column = "user_id", property = "userId")
    List<User> selectByIds(@Param("array") Integer[] userId);

    /**
     * （一对多）根据用户id查询所有对应订单
     * 第一种方式：子查询（没有订单的用户也会查出来）
     * javaType属性可以省略不写 MyBatis会根据类型处理器自动识别 写多错多
     *
     * @return 用户信息集合
     */
    @Select("select * from user")
    @Results({
            @Result(id = true, column = "user_id", property = "userId"),
            @Result(id = true, column = "user_id",
                    property = "orderList", /*javaType = List.class,*/
                    many = @Many(select = "com.jiarong.mapper.OrderMapper.selectOrderByUserId"))
    })
    List<User> selectOrderListByUserId01();

    /**
     * （一对多）根据用户id查询所有对应订单
     * 第二种方式：连接查询（这里本质还是子查询（结果集和第一种相同），不过剔除了没有订单的用户）
     *
     * @return 用户信息集合
     */
    @Select("select * from user u right join orders o on u.user_id = o.user_id")
    @Results({
            @Result(id = true, column = "user_id", property = "userId"),
            @Result(id = true, column = "user_id", property = "orderList",
                    many = @Many(select = "com.jiarong.mapper.OrderMapper.selectOrderByUserId"))
    })
    List<User> selectOrderListByUserId02();

    /**
     * （一对多）根据用户id查询所有对应订单
     * 第三种方式：多表查询（这里本质还是子查询（结果集和第一种相同），不过剔除了没有订单的用户）
     *
     * @return 用户信息集合
     */
    @Select("select * from user u, orders o where u.user_id = o.user_id")
    @Results({
            @Result(id = true, column = "user_id", property = "userId"),
            @Result(id = true, column = "user_id", property = "orderList",
                    many = @Many(select = "com.jiarong.mapper.OrderMapper.selectOrderByUserId"))
    })
    List<User> selectOrderListByUserId03();

    /**
     * （多对多）根据用户id查询所有对应角色
     *
     * @return 用户信息集合
     */
    @Select("select * from user")
    @Results({
            @Result(id = true, column = "user_id", property = "userId"),
            @Result(column = "user_id", property = "roleList",
                    many = @Many(select = "com.jiarong.mapper.RoleMapper.selectRoleByUserId"))
    })
    List<User> selectRoleListByUserId();
}
